﻿/****************************************************************************
Copyright (c) 2014-2015 凌惊雪 微信:Lingjingxue 邮箱:lingjingxue@sina.com QQ:271487457

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/

using ServerPublic.PubObject;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static UtilPublic.Redis.ServerRedis;
using static ServerBase.DataBaseRedis.RdsTxt;
using static ServerBase.Config.Conf;
using static ServerPublic.Data.PubDataMethodMgr;
using UtilPublic.ServerLoger;
using UtilLib;
using ServerBase.Protocol;
using static UtilPublic.Serialization.Jsh;
using ServerBase.Service;
using ServerBase.DataBaseRedis;
using ServerBase.Config;
using ServerPublic.Manager;
using UtilPublic.Serialization;

namespace ServerPublic.Data
{
    /// <summary>
    /// 负载
    /// </summary>
    public static class PubDataLoadMgr
    {
        //服务器负载 
        public static Dictionary<int, CLS_ServerLoadInfo> DictServerGame = new Dictionary<int, CLS_ServerLoadInfo>();
        public static void 获取负载列表游戏服务器()
        {
            DictServerGame.Clear();
            List<string> result = new List<string>();
            ASetMembers(RdsLoadGameSet, ref result);
            foreach (var item in result)
            {
                string data = AKvGet($"{RdsLoadGameKv}:{item}");
                if (Jsh.J2O(data, out CLS_ServerLoadInfo node))
                {
                    DictServerGame[node.ConfigId] = node;
                }
            }
        }
        public static void GetDictServer(long Uuid, ref Dictionary<int, CLS_ServerGameInfo> DictServer)
        {
            foreach (var conf in ConfServerGame.Values)
            {
                if ((EServerType)conf.ServerType != EServerType.游戏)
                {
                    continue;
                }
                var ServerPlatform = (EServerPlatform)conf.ServerPlatform;
                if (BaseServerInfo.ServerPlatform == EServerPlatform.开发
                    || BaseServerInfo.ServerPlatform == EServerPlatform.内网测试)
                {
                    if (ServerPlatform != EServerPlatform.开发
                    && ServerPlatform != EServerPlatform.内网测试)
                    {
                        continue;
                    }
                }
                else
                {
                    if (ServerPlatform != BaseServerInfo.ServerPlatform)
                    {
                        continue;
                    }
                }
                var node = new CLS_ServerGameInfo()
                {
                    ConfigId = conf.Id,
                    ServerIp = conf.ServerIp,
                    ServerPort = conf.ServerPort,
                    ServerState = EServerState.关闭,
                    ServerArea = conf.ServerArea,
                    ServerName = conf.ServerName,
                    ServerStatus = conf.ServerStatus,
                    Recommend = conf.Recommend,
                    NewSever = conf.NewSever,
                    //Recommend = 0,
                    //NewSever = 0,
                };
                //是否开启
                if (DictServerGame.TryGetValue(node.ConfigId, out var item))
                {
                    node.ServerState = item.ServerState;
                    node.CountOnline = item.CountOnline;
                }
                //玩家
                var Puid = PubUserMgr.GetUuidPuid(Uuid, node.ConfigId);
                if (Puid != 0)
                {
                    node.PlayerName = PubPlayerMgr.GetName(Puid);
                    node.PlayerLevel = PubPlayerMgr.GetLevel(Puid);
                }
                DictServer[node.ConfigId] = node;
                //loger.Debug($"{node.ConfigId} {node.ServerState}");
            }
            //if (DictServer.Count > 0)
            //{
            //    try
            //    {
            //        var node = DictServer.Values.Last(it => it.ServerState == EServerState.开启);
            //        if (node != null)
            //        {
            //            node.Recommend = 1;
            //            node.NewSever = 1;
            //        }
            //    }
            //    catch { }
            //}
        }
    }

}